package org.hanyx;

import io.grpc.ManagedChannel;
import org.hanyx.grpc.call.api.CallRequest;
import org.hanyx.grpc.call.api.CallResponse;
import org.hanyx.grpc.call.api.CallServiceGrpc;
import org.hanyx.grpc.hello.api.HelloRequest;
import org.hanyx.grpc.hello.api.HelloResponse;
import org.hanyx.grpc.hello.api.HelloServiceGrpc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(scanBasePackages = "org.hanyx")
public class Application implements CommandLineRunner{

	@Autowired
	private GrpcClient grpcClient;

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		ManagedChannel channel = grpcClient.buildChannel();
		try {
			HelloServiceGrpc.HelloServiceBlockingStub helloService = HelloServiceGrpc.newBlockingStub(channel);
			HelloRequest request = HelloRequest
					.newBuilder()
					.setName("world")
					.build();

			CallServiceGrpc.CallServiceBlockingStub callService = CallServiceGrpc.newBlockingStub(channel);
			CallRequest callRequest = CallRequest
					.newBuilder()
					.setPhone(186000)
					.build();

			CallResponse callResponse = callService.call(callRequest);
			System.out.println(callResponse.getMessage());

			HelloResponse response = helloService.say(request);
			System.out.println(response.getMessage());
		} finally {
			channel.shutdown();
		}

	}
}
